<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表白墙</title>
</head>
<body>
    <div class="container">
        <h1 style="color: green;">添加商品</h1>
        <div class="row">
            <span>商品名称:</span>
            <input type="text" class="edit">
        </div>
        <div class="row">
            <span>商品价格:</span>
            <input type="text" class="edit">
        </div>
        <div class="row">
            <span>商品库存:</span>
            <input type="text" class="edit">
        </div>
        <div class="row">
            <input type="button" value="提交" class="submit">
        </div>
        <!-- 每次点击“提交”，都在下面新增一个 .row，里面放置用户输入的话 -->
        <div class="row">

        </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <script>

        // 1.点击提交按钮的时候，把当前的数据构造成一个HTTP请求，发送给服务器
        let submitButton = document.querySelector('.submit')
        submitButton.onclick = function(){
            // 1.先获取编辑框的内容
            let edits = document.querySelectorAll('.edit');
            let goodName = edits[0].value;
            let goodPrice = edits[1].value;
            let goodNum = edits[2].value;
            console.log(edits);
            if(goodName == ''|| goodPrice == '' || goodNum == ''){
                return;
            }
            for(let i = 0;i < 3;i++){
                edits[i].value = '';
            }
         // 5.构造HTTP请求，把数据发送给服务器保存
            //执行异步http（AJAX）请求
            $.ajax({
                url:"/good/addGood",
                data:{
                    "goodName": goodName,
                    "goodPrice": goodPrice,
                    "goodNum": goodNum
                },
                type:"POST",
                dataType:"json",
                success: function(result){
                    if(result){
                        alert("商品新增成功~")
                        window.location.href = 'order_list.html';
                    }else{
                        alert("商品新增失败~")
                    }
                }
            })
        }

    </script>
    <style>
        *{
            /* 清除浏览器默认样式 */
            margin: 0;
            padding: 0;
            /* 边框和内边距不撑大盒子 */
            box-sizing: border-box;
        }
        .container{
            width: 400px;
            /* 居中对齐 */
            margin: 0 auto;
        }
        h1{
            /* 文本居中 */
            text-align: center;
            /* 上下内边距20px，左右内边距0px */
            padding: 20px 0;
        }
        p{
            /* 文本居中 */
            text-align: center;
            color: #666;
            font-size: 15px;
            padding: 10px 0;
        }
        .row{
            /* 高 */
            height: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        span{
            width: 100px;
            line-height: 40px;
        }
        /* 输入框 */
        .edit{
            /* 宽 */
            width: 300px;
            /* 高 */
            height: 40px;
        }
        /* 提交按钮 */
        .submit{
            /* 宽 */
            width: 300px;
            /* 高 */
            height: 40px;
            /* 字体颜色 */
            color: pink;
            /* 背景颜色 */
            font-size: 20px;
            background-color: black;
            border: none;
            border-radius: 18px;
        }
        .submit:active{
            background-color: orange;
        }
    </style>

</body>
</html>